import { Component, OnInit, Input, SimpleChanges, Output, EventEmitter } from '@angular/core';
import { TurboperHistory } from "../../../../entity/turboper-history.model";
import { TurboperHistoryService } from "../../../../service/turboper-history.service";
import { ConfirmationService, Message } from 'primeng/primeng';
@Component({
  selector: 'app-overhaul-edit',
  templateUrl: './overhaul-edit.component.html',
  styleUrls: ['./overhaul-edit.component.css']
})
export class OverhaulEditComponent implements OnInit {
  @Input() currentHistory: TurboperHistory;
  @Output() colseEdit = new EventEmitter<boolean>()
  operAnalysis: string;
  operProcess: string;

  constructor(
    private turboperHistoryService: TurboperHistoryService,
    private confirmation: ConfirmationService
  ) { }

  ngOnInit() {
  }
  ngOnChanges(changes: SimpleChanges) {

    if (changes['currentHistory'] && changes['currentHistory'].currentValue) {

      this.operProcess = this.currentHistory.operProcess;
      this.operAnalysis = this.currentHistory.operAnalysis;
    }
  }
  onChange() {
    if ((this.operAnalysis != this.currentHistory.operAnalysis) || (this.operProcess != this.currentHistory.operProcess)) {
      return false;
    }
    return true;
  }
  save() {
    this.currentHistory.operAnalysis = this.operAnalysis;
    this.currentHistory.operProcess = this.operProcess;
    this.turboperHistoryService.update(this.currentHistory)
      .then(res => {
        if (res) {
          this.onEditColse(true)
        }
      })
      .catch(err => {
        console.log(err)
      })
  }

  onEditColse(ev?: boolean) {
    if (this.onChange()) return this.colseEdit.emit(ev);
    this.confirmation.confirm({
      header: "提示信息",
      message: '检测到更改是否保存数据?',
      accept: () => {
        this.save()
      },
      reject: () => {
        this.colseEdit.emit(ev)
      }

    });

  }

}



// WEBPACK FOOTER //
// ./src/app/module/data-analysis/farm-overhaul/overhaul-edit/overhaul-edit.component.ts